var LoginIdValue='用户名/邮箱/手机号';
$("#LoginId").val(LoginIdValue).live({
	focus:function(){
		$("#LoginId").select();
	},
	mousedown:function(){
		if(this.value==LoginIdValue){
			this.value='';
		}
	},
	blur:function(){
		if(this.value==''){
			this.value=LoginIdValue;
		}
	}
});
var oldClass = 'strong submit';
function ButtonOver(theTD) {
	oldClass = theTD.className;
	theTD.className = "btn_signin myhover";//btn_signin myhover  btnsignin_hover
}
function ButtonOut(theTD) {
	theTD.className = oldClass;
}
function loadimage() {
	var rand = Math.random();
	var randImage = document.getElementById("randImage");
	if (randImage)
		randImage.src = "image.jsp?" + rand;
}
$().ready(main);
function main() {
	$('form').submit(doSubmit);
	//JudgeSys();
	procLang();
	window.setTimeout(loadimage, 200);
	$(":submit").hover(function() {
		$(this).addClass("btnSubMit_over_0");
	}, function() {
		$(this).removeClass("btnSubMit_over_0");
	});
	$(":text,:password").focus(function() {
		$(this).removeClass().addClass("textInput").addClass("textInput_focus");
	})
	$(":text,:password").blur(function() {
		$(this).removeClass("textInput_focus").addClass("textInput_0");
	})
	$("#smsValid").focus(function() {
		$(this).removeClass().addClass("textInput2").addClass("textInput_focus");
	})
	$("#smsValid").blur(function() {
		$(this).removeClass("textInput_focus").addClass("textInput_0");
	})
}

function procLoad() { //procLoad会比main方法执行得晚一些.
	$('#LoginId').focus();
	initSelRen();
}

function initSelRen() {
	if (isSMS && isOPT) {
		var ranSelRandem = $('#selRandom');
		ranSelRandem
				.change(function() {
					var indiexSe = ranSelRandem[0].selectedIndex;
					if (indiexSe == 0) {
						$("#optValid")
								.replaceWith(
										"<input name='smsValid' id='smsValid' class='textInput2 textInput_0' size='10' maxlength='8' style='margin:0 5px 0 18px;'/><label for='smsValid' class='label2' id='labGET' style='visibility:visible'><a href='javascript:void(0);' onclick='javascript:buildRandomCode();'>点击获取</a></label>");
					} else if (indiexSe == 1) {
						$("#smsValid").replaceWith("<input name='optValid' id='optValid' class='textInput textInput_0' maxlength='8' style='margin:0 5px 0 18px;'/>");
						$("#labGET").replaceWith("");
					}
				})
	}
	if (isSMS == false && isOPT == false) {
		$(".ulLogin").removeClass("ulLogin").addClass("ulLogin2");
	}
}


function procLang() {
	
	var cookieLang = GetCookie('lang');
	if (lang == 'null') {//说明是第一次打开页面没有传任何LangID值，从cookie中找到上次登录时的语言值
		if (cookieLang != null && cookieLang != 'null') {
			lang = cookieLang;
		} else {
			lang = 'zh_CN';
		}
		//changeLang(lang);
	}

	if (lang == 'zh_CN') {
		$('#zhA').css('color', '#990033');
		$('#zhA').css('font-weight', 'bold');
	} else if (lang == 'en_US') {
		$('#enA').css('color', '#990033');
		$('#enA').css('font-weight', 'bold');
	}
}
function doSubmit(o1) {
	var sDat = $("form input").serialize();
	var type = 1;//document.getElementById("loginType").value;
	var loginUrl = buildServUrl('doLoginAction') + '?Action=doLogin&' + sDat + '&loginType=' + type + '&LangID=' + lang;
	//校验用户名和密码字段不能为空begin
	var loginId = document.getElementById("LoginId").value;
	var pwd = document.getElementById("Password").value;
	var smsV = document.getElementById("smsValid");
	var optV = document.getElementById("optValid");
	var validate = document.getElementById("validateCode");

	if (loginId == null || loginId == "") {
		if (type == 1) {
			$('.error_msg').html(jrb.userCanntEmpty);
		} else if (type == 2) {
			alert("EMail地址不能为空。");
		} else if (type == 3) {
			alert("手机号码不能为空。");
		}
		document.loginForm.LoginId.focus();
		document.loginForm.LoginId.select();
		loadimage();
		return false;
	}

	if (pwd == null || pwd == "") {
		$('.error_msg').html(jrb.pwdMsg);
		document.loginForm.password.focus();
		document.loginForm.password.select();
		loadimage();
		return false;
	}

	if (smsV != null && (smsV.value == null || smsV.value == "")) {
		$('.error_msg').html(jrb.duanxinmaNoNull);
		document.loginForm.smsValid.focus();
		document.loginForm.smsValid.select();
		return false;
	} else if (smsV != null && smsV.value != null && smsV.value != "") {
		if (!sendSms)
			$('.error_msg').html(jrb.noSendSms);
		return false;
	}

	if (optV != null && (optV.value == null || optV.value == "")) {
		$('.error_msg').html(jrb.optcheckpwdNoNull);
		document.loginForm.optValid.focus();
		document.loginForm.optValid.select();
		return false;
	}

	if (validate != null && (validate.value == null || validate.value == "")) {
		$('.error_msg').html(jrb.yanzhengmaweikong);
		document.loginForm.validateCode.focus();
		document.loginForm.validateCode.select();
		loadimage();
		return false;
	}
	//end
	$.ajax( {
		url : loginUrl,
		cache : false,
		type : 'GET',
		success : function(j) { //因为声明了dataType :'json',所以此处的j已是一个js对象了.
			var j = eval('(' + j + ')');//转换成JSON对象
		if (j.success) {
			 if(j.success.remain==0){
			         indexUrl = "jsp/urg/userInfo/UpdatePwd.jsp?loginRem=0";              
               window.location.href = doUrl(indexUrl);
			}else{
				$('.error_msg').html(rbstrloadingMsg);
				var indexStringHref = indexString;
				window.location.href = doUrl(basePath + indexStringHref.substring(1));
				// SetCookie('lang', lang);//将该语言写入到cookie中。
			}
	} else if (j.errors) {
		if (!(isSMS || isOPT))
			vali(j.showPic);

		if (j.showPic == true) {
			$('.error_msg').html('当前账号已被冻结，请稍候再试!');
		} else {
			if (j.PwdErrTimes == null || j.PwdErrTimes <= 3) {
				$('.error_msg').html(j.errors[0].msg);
			} else {
				$('.error_msg').html('还可以尝试' + (j.MaxPwdErrTimes - j.PwdErrTimes) + '次，之后账号将被冻结!');
			}
		}

		var targetFlds = document.getElementsByName(j.errors[0].id); //j.errors[0].id 存放的是域的名称,而不是Id
		if (targetFlds && targetFlds.length > 0) {
			targetFlds[0].focus();
		}
	} else {
		$('.error_msg').html(jrb.weizhicuowu);
	}
},
error : function(req, err) {
	$('.error_msg').html(jrb.reqNetErr);
}
	});
	return false; //阻止浏览器再执行submit的动作
	/* 另一种更简便(但自由度降低)用法
	 $.getJSON(loginUrl,function(json){
	  alert(json);    
	}
	);
	return false;*/

	function vali(failed) {
		var randImage = document.getElementById("randImage");
		if (!randImage && failed) {
			$('#signCol').append(vail);
			loadimage();
		}
	}
}
function doUrl(indexUrl) { //为了兼容IIS中跳转的URL中带"/"	
	var indexJspUrl;
	if (indexUrl.indexOf('http') != -1) {
		indexJspUrl = indexUrl;
	} else {
		var pathUrl = path;
		var uChar = indexUrl.substring(0, 1);
		if (uChar == '/')
			indexJspUrl = pathUrl + indexUrl;
		else
			indexJspUrl = pathUrl + '/' + indexUrl;
	}
	if (indexJspUrl.indexOf('#') == -1) {
		indexJspUrl += "";
	}
	return indexJspUrl;
}

function buildRandomCode() {
	var loginId = document.getElementById("LoginId").value;
	var type = 1;
	if (loginId == null || loginId == "") {
		if (type == 1) {
			$('.error_msg').html(jrb.userCanntEmpty);
		} else if (type == 2) {
			alert("EMail地址不能为空。");
		} else if (type == 3) {
			alert("手机号码不能为空。");
		}
		document.loginForm.LoginId.focus();
		document.loginForm.LoginId.select();
		loadimage();
		return false;
	}
	$.ajax( {
		url : getActionUrl('randomSMS'),
		cache : false,
		dataType : 'json',
		error : function() {
		}, //SlideMsg.msg("error","error"); },
		data : {
			'loginId' : loginId
		},
		success : function(j) {
			sendSms = j.SUCCESS == '1' ? true : false;
			$('.error_msg').html(j.MSG);
		}
	});
	return false;
}
/*
 var url = 'jsp/safeInfo/PwdChange.jsp?LangID=' + lang;	
$("#lnk_forgot").attr('href',url);
*/
 $("#lnk_forgot").live('click', function() {
	/*
	var url = 'jsp/safeInfo/PwdChange.jsp?LangID=' + lang;	
	alert(url);
	window.location.href=url;
	return false;
	 */
	// var url = 'usp/safeInfo/PwdChange.jsp?LangID=' + lang;	
	var url = 'usp/safeInfo/reSetPwd.jsp?LangID='+lang;
	$("#lnk_forgot").attr('href',url);
});